package com.oya.worktalk;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Sender;
import com.oya.worktalk.dao.ArticleDao;
import com.oya.worktalk.dao.ArticleDaoImp;
import com.oya.worktalk.dao.UserInfoDaoImp;
import com.oya.worktalk.dto.Article;
import com.oya.worktalk.dto.UserInfo;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	
	@Resource(name="articleDaoImp")                 //���� xml���� <bean id="userDaoImpl" class="com.fureun.mybatis.UserDaoImpl"> �� ���� ������                         //DB �����ϴ� ���� ���� �Ʒ��� ���� �����Ѵ�.
    private ArticleDaoImp articleDao;
	
	@Resource(name="userInfoDaoImp")                 //���� xml���� <bean id="userDaoImpl" class="com.fureun.mybatis.UserDaoImpl"> �� ���� ������                         //DB �����ϴ� ���� ���� �Ʒ��� ���� �����Ѵ�.
	private UserInfoDaoImp userInfoDao;  
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/main.do", method = RequestMethod.GET)
	public @ResponseBody Map<String, Article> home(Locale locale, Model model) {
		logger.info("insert home! The client locale is {}.", locale);
		
		Map<String, Article> map = new HashMap<String, Article>();
		List<Map<String, Article>> list = new ArrayList<Map<String,Article>>();
		list = articleDao.getArticles(10000);
		map = list.get(0);		
		
		return map;
	}
	
	
	@RequestMapping(value = "/add.do", method = RequestMethod.POST)
	@ResponseBody
	public String add(Locale locale, Model model, @RequestParam("deviceid")String deviceid) {
		logger.info("deviceId.", deviceid);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		Article article = new Article(0, 13, deviceid, "ttitle3","content3",0,0,0,10000);
		articleDao.insertArticle(article);
		logger.info("insert finish!!!!!!!!!!", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		ArrayList<String> regid = new ArrayList<String>();
		regid.add(deviceid);
		GCMSender sender = new GCMSender();
		sender.sendGCM(regid);
		return "success";
	}
	
	@RequestMapping(value = "/checkemailid.do", method = RequestMethod.GET)
	@ResponseBody
	public String checkEmailId(Locale locale, Model model, 
			@RequestParam("emailid")String emailid
			) {
		logger.info("emailid", emailid);
		
		int count = userInfoDao.checkEmailId(emailid);
		logger.info("emailid check :: "+ count);
		return "success :: "+count;
	}
	
	@RequestMapping(value = "/signup.do", method = RequestMethod.POST)
	@ResponseBody
	public String signUp(Locale locale, Model model, 
			@RequestParam("emailid")String emailid,
			@RequestParam("password")String password,
			@RequestParam("confirm")String confirm
			) {
		logger.info("emailid", emailid);
		logger.info("password", password);
		logger.info("confirmpass", confirm);
		
//		UserInfo(int emailId, String password, long createDate, long deleteDate)
		UserInfo userInfo = new UserInfo(emailid, password, 0, 0);
		userInfoDao.insertUserInfo(userInfo);
		logger.info("insert signup finish!!!!!!!!!!", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		return "success";
	}
	
}
